import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TeacherInfoMaintenanceGUI extends JFrame implements ActionListener {
    // 组件声明
    private JLabel lblName;          // 姓名标签
    private JTextField txtName;      // 姓名文本框
    private JLabel lblDegree;        // 学位标签
    private JComboBox<String> cbDegree; // 学位下拉框
    private JLabel lblJoinYear;      // 参加年份标签
    private JTextField txtJoinYear;  // 参加年份文本框
    private JLabel lblTitle;         // 职称标签
    private JComboBox<String> cbTitle;  // 职称下拉框
    private JLabel lblGender;        // 性别标签
    private JPanel genderPanel;      // 性别面板
    private JRadioButton rbMale;     // 男单选按钮
    private JRadioButton rbFemale;   // 女单选按钮
    private ButtonGroup bgGender;    // 性别按钮组
    private JPanel buttonPanel;      // 按钮面板
    private JButton btnAddTeacher;   // 录入教师按钮
    private JButton btnPromote;      // 晋升职称按钮
    private JTextField lblPrompt;    // 操作提示文本框
    private JPanel joinYearPanel;    // 参加年份面板

    public TeacherInfoMaintenanceGUI() {
        // 设置窗体属性
        setTitle("教师信息维护系统");
        setBounds(300, 50, 380, 250); // 高度调整为250以适应换行
        setLayout(new FlowLayout());   // 流式布局
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 初始化组件
        initComponents();

        // 添加组件到窗体
        addComponentsToFrame();
    }

    private void initComponents() {
        // 姓名组件
        lblName = new JLabel("姓名:");
        txtName = new JTextField(15);

        // 学位组件
        lblDegree = new JLabel("学位:");
        String[] degrees = {"学士", "硕士", "博士"};
        cbDegree = new JComboBox<>(degrees);

        // 参加年份组件（单独面板）
        joinYearPanel = new JPanel();
        lblJoinYear = new JLabel("参加年份:");
        txtJoinYear = new JTextField(15);
        joinYearPanel.add(lblJoinYear);
        joinYearPanel.add(txtJoinYear);

        // 职称组件
        lblTitle = new JLabel("职称:");
        String[] titles = {"助教", "讲师", "副教授", "教授","校长"};
        cbTitle = new JComboBox<>(titles);

        // 性别组件
        lblGender = new JLabel("性别:");
        genderPanel = new JPanel();
        rbMale = new JRadioButton("男", true); // 默认选中"男"
        rbFemale = new JRadioButton("女");
        bgGender = new ButtonGroup();
        bgGender.add(rbMale);
        bgGender.add(rbFemale);
        genderPanel.add(lblGender);
        genderPanel.add(rbMale);
        genderPanel.add(rbFemale);

        // 按钮组件
        buttonPanel = new JPanel();
        btnAddTeacher = new JButton("录入教师");
        btnPromote = new JButton("晋升职称");
        buttonPanel.add(btnAddTeacher);
        buttonPanel.add(btnPromote);

        // 操作提示
        lblPrompt = new JTextField("操作提示:", 30);
        lblPrompt.setForeground(Color.RED);
        lblPrompt.setEditable(false); // 设置为不可编辑

        // 添加事件监听
        btnAddTeacher.addActionListener(this);
        btnPromote.addActionListener(this);
    }

    private void addComponentsToFrame() {
        // 添加组件到窗体
        add(lblName);
        add(txtName);
        add(lblDegree);
        add(cbDegree);
        add(joinYearPanel);  // 添加参加年份面板（单独一行）
        add(lblTitle);
        add(cbTitle);
        add(genderPanel);    // 添加性别面板
        add(buttonPanel);    // 添加按钮面板
        add(lblPrompt);      // 添加操作提示
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnAddTeacher) {
            addTeacher();
        } else if (e.getSource() == btnPromote) {
            promoteTitle();
        }
    }

    private void addTeacher() {
        // 获取输入信息
        String name = txtName.getText();
        String degree = (String) cbDegree.getSelectedItem();
        String joinYear = txtJoinYear.getText();
        String title = (String) cbTitle.getSelectedItem();
        String gender = rbMale.isSelected() ? "男" : "女";

        // 简单验证
        if (name.isEmpty() || joinYear.isEmpty()) {
            lblPrompt.setText("请输入完整的教师信息！");
            return;
        }

        // 显示操作结果
        lblPrompt.setText(String.format(
                "已录入: %s, %s, %s年入职, %s, %s",
                name, degree, joinYear, title, gender
        ));
    }

    private void promoteTitle() {
        // 获取当前职称
        String currentTitle = (String) cbTitle.getSelectedItem();
        String newTitle = currentTitle;

        // 晋升逻辑
        switch (currentTitle) {
            case "助教":
                newTitle = "讲师";
                break;
            case "讲师":
                newTitle = "副教授";
                break;
            case "副教授":
                newTitle = "教授";
                break;
            case "教授":
                newTitle = "校长";
                return;
            case "校长":
                lblPrompt.setText("已经是最高职称，无法再晋升！");
                return;
        }

        // 更新职称
        cbTitle.setSelectedItem(newTitle);
        lblPrompt.setText("职称已从 " + currentTitle + " 晋升为 " + newTitle);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TeacherInfoMaintenanceGUI gui = new TeacherInfoMaintenanceGUI();
            gui.setVisible(true);
        });
    }
}